﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;

/// <summary>
/// 生成DAL层
/// </summary>
internal class CAutoDAL
{
    /// <summary>
    /// 生成数据持久层
    /// </summary>
    /// <param name="db_name"> 数据库名 </param>
    /// <param name="tablename"> 表名 </param>
    /// <param name="db"></param>
    /// <returns></returns>
    public static string CreateDAL(string db_name, string tablename, CDatabase db)
    {
        string str_namespace = Cconst.DefaultNameSpace;         // 默认命名空间
        string auther = Cconst.DefaultAuther;                   // 默认作者

        return CreateDAL(db_name, tablename, str_namespace, auther, db);
    }

    /// <summary>
    /// 生成数据持久层
    /// </summary>
    /// <param name="db_name"> 数据库名 </param>
    /// <param name="tablename"> 表名 </param>
    /// <param name="str_namespace"> 命名空间 </param>
    /// <param name="db"></param>
    /// <returns></returns>
    public static string CreateDAL(string db_name, string tablename, string str_namespace, string auther, CDatabase db)
    {
        if (str_namespace == "")
            str_namespace = Cconst.DefaultNameSpace;         // 默认命名空间

        if (auther == "")
            auther = Cconst.DefaultAuther;                   // 默认作者

        // 新建目录
        string fileName = CUtility.GetTableName(tablename);
        string directoryPath = CUtility.GetBaseDirectory() + db_name + "\\DAL\\";
        bool exists = CFile.CreateDirectory(directoryPath);

        // 添加文件
        string filePath = CUtility.GetBaseDirectory() + db_name + "\\DAL\\" + fileName + "DAL.cs";

        string keyName = CUtility.GetKeyNameByTableName(tablename, db);
        string fieldsname = "";         // 如: int id, string name, string namePy...
        string parmname = "";           // 如: parms.Add(new SqlParameter("@id",id));\rparms.Add(new SqlParameter("@name",name));... 

        if (exists)
        {
            string p_listName = "p_" + tablename.Replace("t_", "").Replace("T_", "") + "_list";
            string p_saveName = "p_" + tablename.Replace("t_", "").Replace("T_", "") + "_save";
            string p_detailName = "p_" + tablename.Replace("t_", "").Replace("T_", "") + "_detail";
            string p_deleteName = "p_" + tablename.Replace("t_", "").Replace("T_", "") + "_delete";

            StreamWriter w = new StreamWriter(filePath, true, Encoding.Default);
            w.WriteLine(@"/*======================================*\");
            w.WriteLine("* Author: " + auther);
            w.WriteLine("* LastModifyUser: " + auther);
            w.WriteLine("* Description: ");
            w.WriteLine("* FileName：" + fileName + "DAL");
            w.WriteLine("* CreateTime： " + DateTime.Now.ToString());
            w.WriteLine("* Remark: 数据访问层");
            w.WriteLine(@"\*======================================*/");

            w.WriteLine("using System;");
            w.WriteLine("using System.Text;");
            w.WriteLine("using System.Data;");
            w.WriteLine("using System.Data.SqlClient;");
            w.WriteLine("using System.Collections.Generic;\r");
            w.WriteLine("namespace " + str_namespace + "\r{");
            w.WriteLine("   public class " + fileName + "DAL\r   {");

            #region 公用方法
            w.WriteLine("       #region 公用方法");
           
            // p_tablename_list 返回 list 方法
            w.WriteLine("       /// <summary>");
            w.WriteLine("       /// 分页查询");
            w.WriteLine("       /// </summary>");
            w.WriteLine("       /// <param name=\"page\">当前页</param>");
            w.WriteLine("       /// <param name=\"pagesize\">每页显示多少</param>");
            w.WriteLine("       /// <param name=\"condition\">查询条件, 如 \" where 1=1 and id=222\"</param>");
            w.WriteLine("       /// <param name=\"total\">总记录数</param>");
            w.WriteLine("       /// <param name=\"db\">数据库连接对象</param>");
            w.WriteLine("       /// <returns>返回List实体</returns>");
            w.WriteLine("       public static List<" + fileName + "Entity> " + p_listName + "(int page, int pagesize, string condition, ref int total)");
            w.WriteLine("       {");
            w.WriteLine("           List<SqlParameter> parms = new List<SqlParameter>();");
            w.WriteLine("           parms.Add(new SqlParameter(\"@page\",page));");
            w.WriteLine("           parms.Add(new SqlParameter(\"@pagesize\",pagesize));");
            w.WriteLine("           parms.Add(new SqlParameter(\"@condition\",condition));\r");
            w.WriteLine("           SqlParameter pTotal = new SqlParameter(\"@total\", SqlDbType.Int, 4);");
            w.WriteLine("           pTotal.Direction = ParameterDirection.InputOutput;");
            w.WriteLine("           pTotal.Value = total;");
            w.WriteLine("           parms.Add(pTotal);\r");
            w.WriteLine("           SqlParameter pRetval = new SqlParameter(\"@retval\", SqlDbType.Int, 4);");
            w.WriteLine("           pRetval.Direction = ParameterDirection.ReturnValue;");
            w.WriteLine("           parms.Add(pRetval);\r");
            w.WriteLine("           _Database db = new _Database(\"" + db_name + "\");\r");            
            w.WriteLine("           db.fetch_procedure(\"" + p_listName + "\",parms);");
            w.WriteLine("           total= ( pTotal.Value == System.DBNull.Value ) ? 0 : Convert.ToInt32( pTotal.Value );");
            w.WriteLine("           int retval = ( pRetval.Value == System.DBNull.Value ) ? 0 : Convert.ToInt32(pRetval.Value);\r");
            w.WriteLine("           List<" + fileName + "Entity> list = new List<" + fileName + "Entity>() ;");
            w.WriteLine("           for (int i = 0;i < db.num_rows ;i++ ) ");
            w.WriteLine("           {");
            w.WriteLine("               " + fileName + "Entity c = new " + fileName + "Entity();");

            string sql = "sp_COLUMNS '" + tablename + "'";
            DataTable dt_columns = db.ExecuteSql(sql);

            string column_name = "";        // 字段名称
            string column_typename = "";    // varchar,int,datetime,float...
            string column_length = "";      // 长度
            for (int i = 0; i < dt_columns.Rows.Count; i++)
            {
                column_name = dt_columns.Rows[i]["column_name"].ToString();
                column_typename = dt_columns.Rows[i]["type_name"].ToString();
                column_length = dt_columns.Rows[i]["length"].ToString();

                switch (column_typename)
                {
                    case "int":
                    case "int identity":
                        w.WriteLine("               c." + CUtility.GetTableName(column_name) + " = (db.rows[i][\"" + column_name + "\"] == System.DBNull.Value) ? 0 : Convert.ToInt32(db.rows[i][\"" + column_name + "\"].ToString());");
                        break;
                    case "bigint":                    
                        w.WriteLine("               c." + CUtility.GetTableName(column_name) + " = (db.rows[i][\"" + column_name + "\"] == System.DBNull.Value) ? 0 : Convert.ToInt64(db.rows[i][\"" + column_name + "\"].ToString());");
                        break;
                    case "tinyint":
                        w.WriteLine("               c." + CUtility.GetTableName(column_name) + " = (db.rows[i][\"" + column_name + "\"] == System.DBNull.Value) ? Convert.ToByte(0) : Convert.ToByte(db.rows[i][\"" + column_name + "\"].ToString());");
                        break;
                    case "smallint":
                        w.WriteLine("               c." + CUtility.GetTableName(column_name) + " = (db.rows[i][\"" + column_name + "\"] == System.DBNull.Value) ? Convert.ToInt16(0) : Convert.ToInt16(db.rows[i][\"" + column_name + "\"].ToString());");
                        break;
                    case "char":
                    case "nchar":
                    case "varchar":
                    case "nvarchar":
                    case "text":
                    case "ntext":
                    case "xml":
                        w.WriteLine("               c." + CUtility.GetTableName(column_name) + " = (db.rows[i][\"" + column_name + "\"] == System.DBNull.Value) ? \"\" : db.rows[i][\"" + column_name + "\"].ToString();");
                        break;
                    case "float":
                        w.WriteLine("               c." + CUtility.GetTableName(column_name) + " = (db.rows[i][\"" + column_name + "\"] == System.DBNull.Value) ? 0 : Convert.ToSingle(db.rows[i][\"" + column_name + "\"].ToString());");
                        break;
                    case "double":
                        w.WriteLine("               c." + CUtility.GetTableName(column_name) + " = (db.rows[i][\"" + column_name + "\"] == System.DBNull.Value) ? 0 : Convert.ToDouble(db.rows[i][\"" + column_name + "\"].ToString());");
                        break;
                    case "smalldatetime":
                    case "datetime":
                        w.WriteLine("               c." + CUtility.GetTableName(column_name) + " = (db.rows[i][\"" + column_name + "\"] == System.DBNull.Value) ? DateTime.MinValue : Convert.ToDateTime(db.rows[i][\"" + column_name + "\"].ToString());");
                        break;
                    case "bit":
                        w.WriteLine("               c." + CUtility.GetTableName(column_name) + " = (db.rows[i][\"" + column_name + "\"] == System.DBNull.Value) ? false : Convert.ToBoolean(db.rows[i][\"" + column_name + "\"].ToString());");
                        break;
                    case "decimal":
                    case "momey":
                    case "numeric":
                    case "smallmoney":
                        w.WriteLine("               c." + CUtility.GetTableName(column_name) + " = (db.rows[i][\"" + column_name + "\"] == System.DBNull.Value) ? 0 : Convert.ToDouble(db.rows[i][\"" + column_name + "\"].ToString());");
                        break;
                }               
            }
            w.WriteLine("               list.Add(c); ");
            w.WriteLine("           }");
            w.WriteLine("           return list;");
            w.WriteLine("       }\r");

            // p_tablename_list 返回 DataTable 方法            
            w.WriteLine("       /// <summary>");
            w.WriteLine("       /// 分页查询");
            w.WriteLine("       /// </summary>");
            w.WriteLine("       /// <param name=\"page\">当前页</param>");
            w.WriteLine("       /// <param name=\"pagesize\">每页显示多少</param>");
            w.WriteLine("       /// <param name=\"condition\">查询条件, 如 \" where 1=1 and id=222\"</param>");
            w.WriteLine("       /// <param name=\"total\">总记录数</param>");
            w.WriteLine("       /// <param name=\"db\">数据库连接对象</param>");
            w.WriteLine("       /// <returns>返回DataTable</returns>");          
            w.WriteLine("       public static DataTable " + p_listName + "_table(int page, int pagesize, string condition, ref int total)");
            w.WriteLine("       {");
            w.WriteLine("           List<SqlParameter> parms = new List<SqlParameter>();");
            w.WriteLine("           parms.Add(new SqlParameter(\"@page\",page));");
            w.WriteLine("           parms.Add(new SqlParameter(\"@pagesize\",pagesize));");
            w.WriteLine("           parms.Add(new SqlParameter(\"@condition\",condition));\r");
            w.WriteLine("           SqlParameter pTotal = new SqlParameter(\"@total\", SqlDbType.Int, 4);");
            w.WriteLine("           pTotal.Direction = ParameterDirection.InputOutput;");
            w.WriteLine("           pTotal.Value = total;");
            w.WriteLine("           parms.Add(pTotal);\r");
            w.WriteLine("           SqlParameter pRetval = new SqlParameter(\"@retval\", SqlDbType.Int, 4);");
            w.WriteLine("           pRetval.Direction = ParameterDirection.ReturnValue;");
            w.WriteLine("           parms.Add(pRetval);\r");
            w.WriteLine("           DataTable dt = null;\r");
            w.WriteLine("           _Database db = new _Database(\"" + db_name + "\");\r");  
            w.WriteLine("           dt = db.GetDataTable(\""+ p_listName + "\",parms);");
            w.WriteLine("           total = (pTotal.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pTotal.Value);");
            w.WriteLine("           int retval = ( pRetval.Value == System.DBNull.Value ) ? 0 : Convert.ToInt32(pRetval.Value); ");
            w.WriteLine("           return dt;");
            w.WriteLine("       }\r");

            // p_tablename_save 方法
            sql = "select name,type_name(xusertype)as typename,isoutparam from syscolumns where id = object_id('" + p_saveName + "')";
            DataTable dt_proce_columns = db.ExecuteSql(sql);    // 过程的所有参数

            if (dt_proce_columns != null && dt_proce_columns.Rows.Count > 0)
            {
                fieldsname = "";
                parmname = "";
                int isoutparam = 0;
                for (int j = 0; j < dt_proce_columns.Rows.Count; j++)
                {
                    try
                    {
                        isoutparam = Convert.ToInt32(dt_proce_columns.Rows[j]["isoutparam"].ToString());
                    }
                    catch { }

                    if (isoutparam == 1)
                    {
                        fieldsname += "ref " + CUtility.SqlDataTypeToCodeType(dt_proce_columns.Rows[j]["typename"].ToString()) + " " + dt_proce_columns.Rows[j]["name"].ToString().Replace("@", "");
                    }
                    else
                    {
                        fieldsname += CUtility.SqlDataTypeToCodeType(dt_proce_columns.Rows[j]["typename"].ToString()) +
                                      " " + dt_proce_columns.Rows[j]["name"].ToString().Replace("@", "");

                        parmname += "           parms.Add(new SqlParameter(\"" +
                                    dt_proce_columns.Rows[j]["name"].ToString() + "\"," +
                                    dt_proce_columns.Rows[j]["name"].ToString() + "));\r";
                    }

                    if (j < dt_proce_columns.Rows.Count - 1)
                        fieldsname += ",";
                }

                w.WriteLine("       /// <summary>");
                w.WriteLine("       /// 新增|修改");
                w.WriteLine("       /// </summary>");           
                w.WriteLine("       /// <returns></returns>");
                w.WriteLine("       public static void " + p_saveName + "(" + fieldsname + ")");
                w.WriteLine("       {");
                w.WriteLine("           List<SqlParameter> parms = new List<SqlParameter>();\r");
                w.WriteLine(parmname);
                w.WriteLine("           SqlParameter pOutmsg = new SqlParameter(\"@outmsg\", SqlDbType.VarChar, 256);");
                w.WriteLine("           pOutmsg.Direction = ParameterDirection.Output;");
                w.WriteLine("           parms.Add(pOutmsg);\r");
                w.WriteLine("           SqlParameter pNewid = new SqlParameter(\"@newid\", SqlDbType.Int, 4);");
                w.WriteLine("           pNewid.Direction = ParameterDirection.Output;");
                w.WriteLine("           parms.Add(pNewid);\r");
                w.WriteLine("           SqlParameter pRetval = new SqlParameter(\"@retval\", SqlDbType.Int, 4);");
                w.WriteLine("           pRetval.Direction = ParameterDirection.ReturnValue;");
                w.WriteLine("           parms.Add(pRetval);\r");
                w.WriteLine("           _Database db = new _Database(\"" + db_name + "\");\r");  
                w.WriteLine("           db.execute_procedure(\"" + p_saveName + "\",parms);");
                w.WriteLine("           outmsg= ( pOutmsg.Value == System.DBNull.Value ) ? \"\" : Convert.ToString( pOutmsg.Value );");
                w.WriteLine("           newid= ( pNewid.Value == System.DBNull.Value ) ? 0 : Convert.ToInt32( pNewid.Value );");
                w.WriteLine("           int retval = ( pRetval.Value == System.DBNull.Value ) ? 0 : Convert.ToInt32(pRetval.Value);");
                w.WriteLine("       }\r");
            }

            // p_tablename_detail方法,返回实体
            sql = "select name,type_name(xusertype)as typename,isoutparam from syscolumns where id = object_id('" + p_detailName + "')";
            dt_proce_columns = db.ExecuteSql(sql);    // 过程的所有参数

            if (dt_proce_columns != null && dt_proce_columns.Rows.Count > 0)
            {
                fieldsname = "";
                parmname = "";
                for (int j = 0; j < dt_proce_columns.Rows.Count; j++)
                {
                    fieldsname += CUtility.SqlDataTypeToCodeType(dt_proce_columns.Rows[j]["typename"].ToString()) + " " + dt_proce_columns.Rows[j]["name"].ToString().Replace("@", "");
                    parmname += "           parms.Add(new SqlParameter(\"" + dt_proce_columns.Rows[j]["name"].ToString() + "\"," + dt_proce_columns.Rows[j]["name"].ToString() + "));\r";

                    if (j < dt_proce_columns.Rows.Count - 1)
                        fieldsname += ",";
                }
                w.WriteLine("       /// <summary>");
                w.WriteLine("       /// 查询详情");
                w.WriteLine("       /// </summary>");
                w.WriteLine("       /// <returns>返回实体信息</returns>");
                w.WriteLine("       public static " + fileName + "Entity " + p_detailName + "(" + fieldsname + ")");
                w.WriteLine("       {");
                w.WriteLine("           List<SqlParameter> parms = new List<SqlParameter>();\r");
                w.WriteLine(parmname);
                w.WriteLine("           SqlParameter pRetval = new SqlParameter(\"@retval\", SqlDbType.Int, 4);");
                w.WriteLine("           pRetval.Direction = ParameterDirection.ReturnValue;");
                w.WriteLine("           parms.Add(pRetval);\r");
                w.WriteLine("           _Database db = new _Database(\"" + db_name + "\");\r");  
                w.WriteLine("           db.fetch_procedure(\"" + p_detailName + "\",parms);\r");
                w.WriteLine("           int retval = ( pRetval.Value == System.DBNull.Value ) ? 0 : Convert.ToInt32(pRetval.Value); ");
                w.WriteLine("           if(db.num_rows == 0) ");
                w.WriteLine("           {");
                w.WriteLine("               return null;");
                w.WriteLine("           }");
                w.WriteLine("           else");
                w.WriteLine("           {");
                w.WriteLine("               " + fileName + "Entity c = new " + fileName + "Entity();");
                for (int i = 0; i < dt_columns.Rows.Count; i++)
                {
                    column_name = dt_columns.Rows[i]["column_name"].ToString();
                    column_typename = dt_columns.Rows[i]["type_name"].ToString();
                    column_length = dt_columns.Rows[i]["length"].ToString();

                    switch (column_typename)
                    {
                        case "int":
                        case "int identity":
                            w.WriteLine("               c." + CUtility.GetTableName(column_name) + " = (db.rows[0][\"" + column_name + "\"] == System.DBNull.Value) ? 0 : Convert.ToInt32(db.rows[0][\"" + column_name + "\"].ToString());");
                            break;
                        case "bigint":
                            w.WriteLine("               c." + CUtility.GetTableName(column_name) + " = (db.rows[0][\"" + column_name + "\"] == System.DBNull.Value) ? 0 : Convert.ToInt64(db.rows[0][\"" + column_name + "\"].ToString());");
                            break;
                        case "tinyint":
                            w.WriteLine("               c." + CUtility.GetTableName(column_name) + " = (db.rows[0][\"" + column_name + "\"] == System.DBNull.Value) ? Convert.ToByte(0) : Convert.ToByte(db.rows[0][\"" + column_name + "\"].ToString());");
                            break;
                        case "smallint":
                            w.WriteLine("               c." + CUtility.GetTableName(column_name) + " = (db.rows[0][\"" + column_name + "\"] == System.DBNull.Value) ? Convert.ToInt16(0) : Convert.ToInt16(db.rows[0][\"" + column_name + "\"].ToString());");
                            break;
                        case "char":
                        case "nchar":
                        case "varchar":
                        case "nvarchar":
                        case "text":
                        case "ntext":
                        case "xml":
                            w.WriteLine("               c." + CUtility.GetTableName(column_name) + " = (db.rows[0][\"" + column_name + "\"] == System.DBNull.Value) ? \"\" : db.rows[0][\"" + column_name + "\"].ToString();");
                            break;
                        case "float":
                            w.WriteLine("               c." + CUtility.GetTableName(column_name) + " = (db.rows[0][\"" + column_name + "\"] == System.DBNull.Value) ? 0 : Convert.ToSingle(db.rows[0][\"" + column_name + "\"].ToString());");
                            break;
                        case "double":
                            w.WriteLine("               c." + CUtility.GetTableName(column_name) + " = (db.rows[0][\"" + column_name + "\"] == System.DBNull.Value) ? 0 : Convert.ToDouble(db.rows[0][\"" + column_name + "\"].ToString());");
                            break;
                        case "smalldatetime":
                        case "datetime":
                            w.WriteLine("               c." + CUtility.GetTableName(column_name) + " = (db.rows[0][\"" + column_name + "\"] == System.DBNull.Value) ? DateTime.MinValue : Convert.ToDateTime(db.rows[0][\"" + column_name + "\"].ToString());");
                            break;
                        case "bit":
                            w.WriteLine("               c." + CUtility.GetTableName(column_name) + " = (db.rows[0][\"" + column_name + "\"] == System.DBNull.Value) ? false : Convert.ToBoolean(db.rows[0][\"" + column_name + "\"].ToString());");
                            break;
                        case "decimal":
                        case "momey":
                        case "numeric":
                        case "smallmoney":
                            w.WriteLine("               c." + CUtility.GetTableName(column_name) + " = (db.rows[0][\"" + column_name + "\"] == System.DBNull.Value) ? 0 : Convert.ToDouble(db.rows[0][\"" + column_name + "\"].ToString());");
                            break;
                    } 

                }
                w.WriteLine("               return c;");
                w.WriteLine("           }");
                w.WriteLine("       }\r");
            }

            // p_tablename_detail方法,返回 DataTable
            sql = "select name,type_name(xusertype)as typename,isoutparam from syscolumns where id = object_id('" + p_detailName + "')";
            dt_proce_columns = db.ExecuteSql(sql);    // 过程的所有参数

            if (dt_proce_columns != null && dt_proce_columns.Rows.Count > 0)
            {
                fieldsname = "";
                parmname = "";
                for (int j = 0; j < dt_proce_columns.Rows.Count; j++)
                {
                    fieldsname += CUtility.SqlDataTypeToCodeType(dt_proce_columns.Rows[j]["typename"].ToString()) + " " + dt_proce_columns.Rows[j]["name"].ToString().Replace("@", "");
                    parmname += "           parms.Add(new SqlParameter(\"" + dt_proce_columns.Rows[j]["name"].ToString() + "\"," + dt_proce_columns.Rows[j]["name"].ToString() + "));\r";

                    if (j < dt_proce_columns.Rows.Count - 1)
                        fieldsname += ",";
                }
                w.WriteLine("       /// <summary>");
                w.WriteLine("       /// 查询详情");
                w.WriteLine("       /// </summary>");
                w.WriteLine("       /// <returns>返回DataTable</returns>");
                w.WriteLine("       public static DataTable " + p_detailName + "_table(" + fieldsname + ")");
                w.WriteLine("       {");
                w.WriteLine("           List<SqlParameter> parms = new List<SqlParameter>();");
                w.WriteLine(parmname);
                w.WriteLine("           SqlParameter pRetval = new SqlParameter(\"@retval\", SqlDbType.Int, 4);");
                w.WriteLine("           pRetval.Direction = ParameterDirection.ReturnValue;");
                w.WriteLine("           parms.Add(pRetval);\r");
                w.WriteLine("           DataTable dt = null; ");
                w.WriteLine("           _Database db = new _Database(\"" + db_name + "\");\r");  
                w.WriteLine("           dt = db.GetDataTable(\"" + p_detailName + "\",parms);");
                w.WriteLine("           int retval = ( pRetval.Value == System.DBNull.Value ) ? 0 : Convert.ToInt32(pRetval.Value); ");
                w.WriteLine("           return dt;");
                w.WriteLine("       }\r");
            }

            // p_tablename_delete方法
            sql = "select name,type_name(xusertype)as typename,isoutparam from syscolumns where id = object_id('" + p_deleteName + "')";
            dt_proce_columns = db.ExecuteSql(sql);    // 过程的所有参数

            if (dt_proce_columns != null && dt_proce_columns.Rows.Count > 0)
            {
                fieldsname = "";
                parmname = "";
                int isoutparam = 0;
                for (int j = 0; j < dt_proce_columns.Rows.Count; j++)
                {
                    try
                    {
                        isoutparam = Convert.ToInt32(dt_proce_columns.Rows[j]["isoutparam"].ToString());
                    }
                    catch { }

                    if (isoutparam == 1)
                        fieldsname += "ref " + CUtility.SqlDataTypeToCodeType(dt_proce_columns.Rows[j]["typename"].ToString()) + " " + dt_proce_columns.Rows[j]["name"].ToString().Replace("@", "");
                    else
                    {
                        fieldsname += CUtility.SqlDataTypeToCodeType(dt_proce_columns.Rows[j]["typename"].ToString()) +
                                      " " + dt_proce_columns.Rows[j]["name"].ToString().Replace("@", "");

                        parmname += "           parms.Add(new SqlParameter(\"" +
                                    dt_proce_columns.Rows[j]["name"].ToString() + "\"," +
                                    dt_proce_columns.Rows[j]["name"].ToString() + "));\r";
                    }

                    if (j < dt_proce_columns.Rows.Count-1)
                        fieldsname += ",";
                }
                w.WriteLine("       /// <summary>");
                w.WriteLine("       /// 删除");
                w.WriteLine("       /// </summary>");                
                w.WriteLine("       public static void " + p_deleteName + "(" + fieldsname + ")");
                w.WriteLine("       {");
                w.WriteLine("           List<SqlParameter> parms = new List<SqlParameter>();\r");
                w.WriteLine(parmname);
                w.WriteLine("           SqlParameter pRetval = new SqlParameter(\"@retval\", SqlDbType.Int, 4);");
                w.WriteLine("           pRetval.Direction = ParameterDirection.ReturnValue;");
                w.WriteLine("           parms.Add(pRetval);\r");
                w.WriteLine("           _Database db = new _Database(\"" + db_name + "\");\r");  
                w.WriteLine("           db.fetch_procedure(\"" + p_deleteName + "\",parms);\r");
                w.WriteLine("           int retval = ( pRetval.Value == System.DBNull.Value ) ? 0 : Convert.ToInt32(pRetval.Value); ");                
                w.WriteLine("       }\r");
            }

            w.WriteLine("       #endregion");
            #endregion

            w.WriteLine("   }");
            w.WriteLine("}");

            w.Flush();
            w.Close();
        }

        return tablename + "DAL.cs生成成功";
    }

}

